package org.an.proj.contacts.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.an.ku.base.adapter.BaseSimpleAdapter;
import org.an.ku.util.ToastUtil;
import org.an.ku.view.MyTextView;
import org.an.proj.contacts.R;
import org.an.proj.contacts.ui.base.BaseActivity;

/**
 * 猜数字游戏
 */
public class GameActivity extends BaseActivity {

  @Bind(R.id.et_input) EditText mEtInput;
  @Bind(R.id.lv_result) ListView mLvResult;
  @Bind(R.id.btn_confirm) TextView mBtnConfirm;

  private String mAnswerNum;
  private int a;
  private int b;
  private List<String> mList;
  private MyAdapter mAdapter;
  private int mCount;
  private final int MAX_COUNT = 9;

  @Override protected boolean autoHideSoftWindow() {
    return false;
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    ButterKnife.bind(this);

    mList = new ArrayList<>();
    mAdapter = new MyAdapter(this);
    mLvResult.setAdapter(mAdapter);
    mAdapter.setDatas(mList);

    reset();
  }

  private void reset() {
    mCount = 0;
    mBtnConfirm.setEnabled(true);
    mAnswerNum = createNum();
    mList.clear();
    mAdapter.notifyDataSetChanged();
  }

  @OnClick(R.id.btn_confirm) public void onConfirm() {
    String text = mEtInput.getText().toString().trim();
    if (!checkNum(text)) {
      ToastUtil.toast(this, "请输入有效的数字");
      return;
    }
    mEtInput.setText("");
    mCount++;

    // 对比两个数字 0425 / 1236
    for (int i = 0; i < mAnswerNum.length(); i++) {
      for (int j = 0; j < text.length(); j++) {
        if (mAnswerNum.charAt(i) == text.charAt(j)) {
          if (i == j) {
            a++;
          } else {
            b++;
          }
          break;
        }
      }
    }
    mList.add(new String(String.format("%s : %dA%dB", text, a, b)));
    mAdapter.notifyDataSetChanged();

    if (a == 4) {
      mBtnConfirm.setEnabled(false);
      ToastUtil.toast(this, "恭喜你答对了:" + mAnswerNum);
      return;
    }

    if (mCount >= 6 && mCount < MAX_COUNT) {
      ToastUtil.toast(this, "你还有%d次机会啦!", MAX_COUNT - mCount);
    }

    if (mCount >= MAX_COUNT) {
      mBtnConfirm.setEnabled(false);
      ToastUtil.toast(this, "你好笨呀:" + mAnswerNum);
      return;
    }

    a = 0;
    b = 0;
  }

  @OnClick(R.id.btn_reset) public void onClick() {
    reset();
  }

  private String createNum() {
    StringBuilder text = new StringBuilder();
    Random random = new Random(System.currentTimeMillis());
    while (text.length() < 4) {
      int num = random.nextInt(10); // [0,10)
      if (!text.toString().contains(String.valueOf(num))) {
        text.append(String.valueOf(num));
      }
    }
    return text.toString();
  }

  private boolean checkNum(@NonNull String text) {
    if (text.length() != 4) {
      return false;
    }

    for (int i = 0; i < text.length(); i++) {
      for (int j = i + 1; j < text.length(); j++) {
        if (text.charAt(i) == text.charAt(j)) {
          return false;
        }
      }
    }

    return true;
  }

  public static class MyAdapter extends BaseSimpleAdapter<String, MyAdapter.ViewHolder> {

    public MyAdapter(Context context) {
      super(context);
    }

    @Override public int getLayoutId() {
      return R.layout.item_game_result;
    }

    @Override public ViewHolder onCreateViewHolder(View convertView) {
      return new ViewHolder(convertView);
    }

    @Override public void onBindViewHolder(ViewHolder holder, String s, int position) {
      holder.mTvResult.setText(s);
    }

    static class ViewHolder {
      @Bind(R.id.tv_result) MyTextView mTvResult;

      ViewHolder(View view) {
        ButterKnife.bind(this, view);
      }
    }
  }
}
